package net.xxcl.exam.controller;

import net.xxcl.exam.pojo.Criteria;
import net.xxcl.exam.pojo.Kemu;
import net.xxcl.exam.service.KemuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 夏汛
 * Date: 11-12-13
 * Time: 下午9:29
 */
@Controller
@RequestMapping("/kemu")
public class KemuController {
    @Autowired
    private KemuService kemuService;

    /**
     * 获取科目分页
     * @param start
     * @param limit
     * @return
     */
    @RequestMapping(value = "/getKemus",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> getKemus(@RequestParam("start") Integer start, @RequestParam("limit") Integer limit){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        int total = kemuService.countByExample(criteria);
        criteria.setMysqlOffset(start);
        criteria.setMysqlLength(limit);
        List<Kemu> list = kemuService.selectByExample(criteria);
        responseMap.put("total",total);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 获取科目的下拉列表
     * @return
     */
    @RequestMapping(value = "/getAllKemus",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> getAllKemus(){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        List<Kemu> list = kemuService.selectByExample(criteria);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 新增和修改科目
     * @param kemu
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> saveOrUpdate(@ModelAttribute("kemu") Kemu kemu){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try{
            //新增科目
            if(kemu.getId()==null){
                kemuService.insert(kemu);
                responseMap.put("method", "Create");
                responseMap.put("success", "true");
                responseMap.put("info", "新增成功！");
            }
            //编辑科目
            else {
                kemuService.updateByPrimaryKey(kemu);
                responseMap.put("success", "true");
                responseMap.put("info", "编辑成功！");
            }
        } catch (Exception e){
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
        }
        return responseMap;
    }
    
    @RequestMapping(value="/remove",method=RequestMethod.POST)
    public @ResponseBody Map<String,Object> remove(@RequestParam("ids") String ids){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try {
            kemuService.deleteKemu(ids);
            responseMap.put("success", "true");
            responseMap.put("info", "删除成功！");
            return responseMap;
        }catch(Exception e) {
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
            return responseMap;
        }
    }
}
